// pages/goods/goods.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navState: 0, //导航状态
  },
  //点击导航
  navSwitch: function (e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.setData({
      navState: index
    })
  },
  search: function (e) {
    wx.navigateTo({
      url: '/pages/news-search/news-search',
    })
  },
  type(e) {
    var typename = e.currentTarget.dataset.typename
    console.log(typename)
    getApp().globalData.typename = typename
    wx.switchTab({
      url: '/pages/news/news',
    })
  },
  goodsinfo(e) {
    var id = e.currentTarget.dataset.id
    // console.log(id)
    var popularity = e.currentTarget.dataset.popularity
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/shopping_popularity.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        id: id,
        popularity: popularity
      },
      success: function (res) {
        console.log(res.data)
        wx.navigateTo({
          url: '/pages/news-info/news-info?id=' + id + '',
        })
        that.setData({

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

  },
  result(e) {
    let name = getApp().globalData.usersname
    var time = util.formatTime(new Date());
    console.log(name, time)
    var num = Math.floor(Math.random() * 100000000000000 + 100000000000000)
    console.log(num)
    var id = e.currentTarget.dataset.id
    var img = e.currentTarget.dataset.img
    var goodsname = e.currentTarget.dataset.goodsname
    var news = e.currentTarget.dataset.news
    var price = e.currentTarget.dataset.price
    var shop = e.currentTarget.dataset.shop
    var number = e.currentTarget.dataset.number
    var that = this;
    if (!name) {
      console.log('抱歉，您还未登录！')
      wx.showModal({
        title: '提示',
        content: '是否进行用户登录',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            console.log('登录')
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else { //这里是点击了取消以后
            console.log('取消')
          }
        }
      })
    } else {
      wx.showModal({
        title: '',
        content: '是否立即抢购该商品',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.request({
              url: 'http://localhost/shopping/goods_num_add.php',
              header: {
                'Content-Type': 'application/json'
              },
              data: {
                id: id,
                num: number,
              },
              success: function (res) {
                console.log(res.data)

              },
              fail: function (res) {
                console.log("fail");
              }
            })
            wx.request({
              url: 'http://localhost/shopping/admin_activity_add.php',
              header: {
                'Content-Type': 'application/json'
              },
              data: {
                name: name,
                startdate: time,
                ordernumber: num,
                goodsname: goodsname,
                goodsimg: img,
                goodsnews: news,
                // goodsnumber: number,
                goodsprice: price,
                goodsshop: shop,
                goodsid: id,
                num: number
              },
              success: function (res) {
                // console.log(res.data)
                wx.showModal({
                  title: '订单已生成',
                  content: '是否跳转到订单页面',
                  success(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateTo({
                        url: '/pages/order-pay/order-pay',
                      })
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
                that.setData({})
              },
              fail: function (res) {
                console.log("fail");
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }


  },
  Goods: function () {
    var that = this
    wx.request({
      url: 'http://localhost/shopping/admin_activity.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arr: res.data.activity,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_type.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrxx: res.data.type,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

    var time = util.formatTime(new Date());
    var nowtime = Date.parse(time) / 1000
    console.log(nowtime, time)

    var hh = Number(23 - util.formatTimeh(new Date()));
    console.log(hh)
    var mm = Number(59 - util.formatTimem(new Date()));
    console.log(mm)
    var ss = util.formatTimes(new Date());
    console.log(ss)

    that.setData({
      h: hh,
      m: mm,
      s: ss
    })
  },
  Numdown: function () {
    var that = this,
      h = that.data.h,
      m = that.data.m,
      s = that.data.s;
    setTimeout(function () {
      that.setData({
        s: Number(s) - 1
      })
      that.sendNum()
    }, 1000)
    setTimeout(function () {
      that.setData({
        m: Number(m) - 1
      })
      that.mendNum()
    }, 1000 * 60)
    setTimeout(function () {
      that.setData({
        h: Number(h) - 1
      })
      that.endNum()
    }, 1000 * 60 * 60)
  },
  sendNum: function () {
    var that = this,
      s = that.data.s;
    if (s == 0) {
      that.setData({
        s: 59
      })
    } else {
      that.Numdown()
    }
  },
  mendNum: function () {
    var that = this,
      m = that.data.m;
    if (m == 0) {
      that.setData({
        m: 59
      })
    } else {
      that.Numdown()
    }
  },
  hendNum: function () {
    var that = this,
      h = that.data.h;
    if (h == 0) {
      that.setData({
        h: '结',
        m: '束',
        s: '了',
      })
    } else {
      that.Numdown()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Goods();
    var that = this
    that.Numdown();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.Goods();
    var that = this
    that.Numdown();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }



})